<?php
/**
 * Pages model
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Content Module
 * @subpackage Model
 * @license  New BSD License
 *
 */
class Models_Content_Pages extends Bel_Model_Abstract {	
	/**
	 * Defines one-to-many relations between pages table and table that links menus and pages.  
	 *
	 * @var array
	 */
	protected $_dependentTables = array ('Models_Content_Page2menu' );
	
	/**
	 * Constructor to set name of a table with pages in a database and table's primary key.
	 * 
	 * @param  mixed $config Array of user-specified config options - ZendFramework feature.
	 * @return void
	 */
	public function __construct($array = array()) {
		$this->_name = TABLE_PREFIX . 'pages';
		$this->_primary = array ('page_id' );
		parent::__construct ( $array );
	}
	
	/**
	 * Return page by system name. System name is used in URLs.
	 *
	 * @param string $system_name - page system name
	 * @return array - array with page data or false
	 */
	public function getPageBySystemName($system_name) {
		
		if(!$system_name){
			return false;
		}		
		return ($row = $this->fetchRow ( array ('page_system_name = ?' => $system_name ) )) ? $row : false;
	}
	
	/**
	 * Returns page by current URL
	 *
	 * @return Zend_Db_Table_Rowset_Abstract
	 */
	public function getCurrentPage(){		
		$pageUrlOrSysName = trim(str_replace(array('.html',strstr($_SERVER['REQUEST_URI'],'?')),array('',''),$_SERVER['REQUEST_URI']),'/');
		//echo $pageUrlOrSysName.' '.$_SERVER['REQUEST_URI'];
		$curPage = $this->fetchAll('page_system_name=\''.$pageUrlOrSysName.'\' or page_url=\''.$_SERVER['REQUEST_URI'].'\'')->current();		
		return $curPage;
	}
}